package dao

import (
	"Im/global"
	"Im/model"
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
	"time"
)

func GetUserBasicByAccountPwd(account, password string) (*model.UserLogin, error) {
	ub := new(model.UserLogin)
	err := FindOne(ub.CollectionName(), bson.M{
		"account":  account,
		"password": password,
	}, ub)
	if err != nil {
		return nil, err
	}
	return ub, err
}

func GetUserBasicById(Id primitive.ObjectID) (*model.UserBasic, error) {
	ub := new(model.UserBasic)
	err := FindOne(ub.CollectionName(), bson.M{
		"_id": Id,
	}, ub)
	if err != nil {
		return nil, err
	}
	return ub, nil
}

func GetUserBasicByEmail(email string) error {
	ub := new(model.UserBasic)
	return FindOne(ub.CollectionName(), bson.M{
		"email": email,
	}, ub)
}

// 根据用户id和聊天室id来查询，以此来判断用户是否为该聊天室的用户
func GetUserRoomByUserIdRoomId(userId string, roomId string) (*model.UserRoom, error) {
	ur := new(model.UserRoom)
	err := FindOne(model.UserRoom{}.CollectionName(), bson.M{
		"user_id": userId,
		"room_id": roomId,
	}, ur)
	return ur, err
}

// 保存用户发送过来的消息
func SaveUserMsg(userId, roomId, data string) error {
	uMsg := model.MessageBasic{
		UserId:   userId,
		RoomId:   roomId,
		Data:     data,
		CreateAt: time.Now().String(),
		UpdateAt: time.Now().String(),
	}

	err := InsertOne(uMsg.CollectionName(), uMsg)
	return err
}

// 获取特定房间的用户
func GetUserRoomByRoomId(roomId string) ([]*model.UserRoom, error) {
	urs := make([]*model.UserRoom, 0)

	file, err := global.DB.Collection(model.UserRoom{}.CollectionName()).Find(context.Background(), bson.D{
		{"room_id", roomId},
	})
	for file.Next(context.Background()) {
		ur := new(model.UserRoom)
		err = file.Decode(ur)
		if err != nil {
			log.Println(err)
			return nil, err
		}
		urs = append(urs, ur)
	}
	return urs, err
}

// 获取用户的消息列表
func GetUserMsg(userId string, pgSize int64, pgNum int64) ([]*model.MessageBasic, error) {
	uMsgs := make([]*model.MessageBasic, 0)
	findOptions := options.Find()
	findOptions.SetLimit(pgSize)
	findOptions.SetSkip(pgSize * (pgNum - 1))
	file, err := global.DB.Collection(model.MessageBasic{}.CollectionName()).Find(context.TODO(), bson.M{
		"user_id": userId,
	}, findOptions)
	if err != nil {
		return nil, err
	}
	for file.Next(context.TODO()) {
		uMsg := new(model.MessageBasic)
		err = file.Decode(uMsg)
		if err != nil {
			log.Println(err)
			return nil, err
		}
		uMsgs = append(uMsgs, uMsg)
	}
	return uMsgs, err
}

// 用户注册
func SignUp(account, password, nickName, email string) error {
	user := model.UserRegister{
		Account:   account,
		Password:  password,
		Nickname:  nickName,
		Sex:       0,
		Email:     email,
		CreatedAt: time.Now().Unix(),
	}
	return InsertOne(user.CollectionName(), user)
}

//更新用户信息

func UpdateUser(user model.UserBasic) error {
	upFilter := bson.M{
		"_id": user.Id,
	}
	upData := bson.M{
		"avatar":   user.Avatar,
		"nickname": user.Nickname,
	}
	return UpdateOne(user.CollectionName(), upFilter, upData)
}

// 添加好友
func AddFriends(user model.UserAddFriend) error {
	upFilter := bson.M{
		"_id": user.Id,
	}
	upData := bson.M{
		"friend_application": bson.M{
			"type": user.AddType,
			"id":   user.FriendApplication,
		},
	}

	return UpdateArrOne(user.CollectionName(), upFilter, upData)
}

// 添加好友后删除添加申请,删除申请的时候顺便将好友添加了
func DeleteApplication(user model.UserAddFriend) error {
	upFilter := bson.M{
		"_id": user.Id,
	}
	upData := bson.M{
		"friend_application": bson.M{
			"type": user.AddType,
			"id":   user.FriendApplication,
		},
	}
	addFriendsData := bson.M{
		"friend_arr": user.FriendApplication,
	}
	//先添加好友，然后再删除对应的好友申请
	err := UpdateArrOne(user.CollectionName(), upFilter, addFriendsData)
	if err != nil {
		return err
	}

	return DeleteArrOne(user.CollectionName(), upFilter, upData)
}
